<?php
	
	/**
	 * 货品列表控制器
	 */
	 class GoodsListController extends AuthController{
	 	private $model;
		public function __init(){
			$this->model = K('GoodsList');
		}
		
		/**
		 * 检测当前组合是否已经存在
		 */
		 public function isCombineDefine(){
		 	$combine = Q('combine','','strval');
			$combine = implode(',',$combine);
			
			//检测是否存在
			if($this->model->where("combine='{$combine}'")->find()){
				$this->ajax(1);
			}else{
				$this->ajax(0);
			}
		 }
		 
		 /**
		  * 修改时检测当前组合是否存在，不包含当前的的组合
		  */
		  public function editIsCombineDefine(){
		  	 $data = json_decode($_POST['data'],true);
			 //返回一个新值
			 $newData = $this->model->updateCombine($data);
			 if(!$newData){
			 	$this->ajax(array('status'=>0,'message'=>$this->model->error));
			 }else{
			 	$this->ajax(array('status'=>1,'message'=>'修改成功','data'=>$newData));
			 }
			 

			 
		  }
		
		
		/**
		 * 显示当前商品的货品列状况
		 */
		 public function index(){
		 	//商品ID
		 	$gid = Q('get.gid',0,'intval');
			//返回新的一个规格数组 
			$newSpec = $this->model->getGoodsAttr($gid);
//			var_dump($newSpec);
			//返回当前商品所有的库存
			$goodsNum = $this->model->getGoodsNum($gid);
			$this->assign('goodsNum',$goodsNum);
			$this->assign('newSpec',$newSpec);
			$this->display();
		 }
		/**
		 * 删除相应商品列表
		 */
		 public function delGoodsList(){
		 	if(!IS_AJAX) $this->error('非法请求');
		 	$lid = Q('post.lid',0,'intval');
			//获得商品数量和商品ID
			$numGid = $this->model->where("lid={$lid}")->find();//获得当前规格数量
			//更新商品数量
			K('Goods')->dec('goods_count',"gid={$numGid['goods_gid']}",$numGid['goods_number']);
			if($this->model->delete($lid)){
				$this->ajax(array('message'=>'删除成功'));
			}else{
				$this->ajax(array('message'=>'删除失败'));
			}
		 }
		//添加商品列表
		public function addGoodList(){
//			p(Q('post.'));die;
			if($this->model->addGoodsList()){
				$this->success('添加成功');
			}else{
				$this->error($this->model->error);
			}
		}
		
	 }
	